﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BKH.BLL;

namespace BKH.MyUserControl.UCHienThi
{
    public partial class ucBNNoiTru : UserControl
    {
        String ID_Co;
        public ucBNNoiTru()
        {
            InitializeComponent();
            showCbKhoa();
            
        }
        /// <summary>
        /// Hiển thị danh sách khoa lên combobox
        /// </summary>

        void showCbKhoa()
        {
            KhoaBUO khoa = new KhoaBUO();
            cbKhoa.DataSource = khoa.GetAll();
            cbKhoa.DisplayMember = "WardName";
            cbKhoa.ValueMember = "id_Ward";
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Hiển thị danh sách bệnh nhân tương ứng từng khoa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            //int khoaid=int.Parse(cbKhoa.SelectedValue.ToString());
            tbl_Khoa khoa = (tbl_Khoa)cbKhoa.SelectedItem;
            int khoaid = int.Parse(khoa.id_Ward.ToString());
            HospitalDBDataContext context = new HospitalDBDataContext();
            var list = context.tbl_BenhNhanNoiTrus.Join(
                context.tbl_BenhNhan_LanKhams,
                a => a.LanKhamID,
                b => b.LanKhamID,
                (a, b) => new
                {
                    LanKhamID = a.LanKhamID,
                    BenhNhanID = b.BenhNhanID,
                    LoaiKhamID = b.LoaiKhamID,
                    NhanVienID = b.NhanVienID,
                    BedID = a.BedID,

                    Date_Start = a.Date_Start,
                    Date_Finish = a.Date_Finish,
                    ChuanDoan = b.ChuanDoan,
                    NgayKham = b.NgayKham
                }).Join(
                context.tbl_BenhNhans,
                c => c.BenhNhanID,
                d => d.BenhNhanID,
                (c, d) => new
                {
                    LoaiKhamID = c.LoaiKhamID,
                    BenhNhanID = c.BenhNhanID,
                    NhanVienID = c.NhanVienID,
                    Name = d.FirstName + " " + d.LastName,
                    Birthday = d.Birthday,
                    Sex = d.Sex,
                    BedID = c.BedID,
                    Date_Start = c.Date_Start,
                    Date_Finish = c.Date_Finish,
                    ChuanDoan = c.ChuanDoan,
                    NgayKham = c.NgayKham
                }
                ).Join(
                context.tbl_NhanViens,
                g => g.NhanVienID,
                f => f.NhanVienID,
                (g, f) => new
                {
                    LoaiKhamID = g.LoaiKhamID,
                    BenhNhanID = g.BenhNhanID,
                    NhanVienID = g.NhanVienID,
                    KhoaID = f.KhoaID,
                    Name = g.Name,
                    Birthday = g.Birthday,
                    Sex = g.Sex,
                    BedID = g.BedID,
                    Date_Start = g.Date_Start,
                    Date_Finish = g.Date_Finish,
                    ChuanDoan = g.ChuanDoan,
                    NgayKham = g.NgayKham
                }
                ).Join(
                context.tbl_Giuongs,
                h => h.BedID,
                k => k.BedID,
                (h, k) => new
                {
                    LoaiKhamID = h.LoaiKhamID,
                    BenhNhanID = h.BenhNhanID,
                    NhanVienID = h.NhanVienID,
                    KhoaID = h.KhoaID,
                    Name = h.Name,
                    Birthday = h.Birthday,
                    Sex = h.Sex,
                    TenGiuong=k.TenGiuong,
                    Date_Start = h.Date_Start,
                    Date_Finish = h.Date_Finish,
                    ChuanDoan = h.ChuanDoan,
                    NgayKham = h.NgayKham
                }

                )
                .Where(n => n.KhoaID == khoaid && n.LoaiKhamID == (int)LoaiKham.NoiTru
                );
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = list;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void thôngTinNgườiNhàToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                NguoiNhaBUO ngNha = new NguoiNhaBUO();
                tbl_ThanNhan obj = ngNha.GetSingle(int.Parse(ID_Co));
                if (obj.ThanNhanID != null)
                {
                    ucThongTinNguoiNha info = new ucThongTinNguoiNha(int.Parse(ID_Co));
                    ControlAcess.ShowConTrol(this.ParentForm, info, "gbHienThi");
                }

            }
            catch
            {
                MessageBox.Show("Ko có thông tin người nhà của bệnh nhân này", "Thông báo");
            }
        }

        private void lịchSửKhámToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucLichSuKham ls = new ucLichSuKham(int.Parse(ID_Co));
            ControlAcess.ShowConTrol(this.ParentForm, ls, "gbHienThi");
        }

        private void raViệnToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
